﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using GUIServerConnectorAPI;
using System.Data.Entity;
using EntityModel;

namespace LoginForm
{
    public partial class PacientForm : Form
    {
        private LoginForm loginForm;
        private Guid sessionId;

        private BinaryFormatter binaryFormatter;

        private List<DefinitieSenzori> sensorDefinitions;
        
        public PacientForm(LoginForm loginForm)
        {
            InitializeComponent();
            this.loginForm = loginForm;
            binaryFormatter = new BinaryFormatter();
        }

        public void Show(Guid sessionId)
        {
            this.sessionId = sessionId;
            LoadPacientData();
            LoadDoctorsData();
            LoadTreatmentsData();
            LoadSensorsData();
            Show();
        }

        private void PacientForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GUIServerConnector.getInstance().getAuthenticator().logout(sessionId);
            this.Hide();
            loginForm.Reset(this);
        }

        private void PacientForm_Load(object sender, EventArgs e)
        {
            
            Update();
        }

        public void LoadPacientData()
        {
            Pacient pacient = GUIServerConnector.getInstance().getServerInterface().getPacientData(sessionId);
            TXTLastName.Text = pacient.Nume;
            TXTFirstName.Text = pacient.Prenume;
            TXTPIN.Text = pacient.CNP;
            DTPBirthdate.Value = pacient.Data_nasterii;
            CMBSex.SelectedIndex = (pacient.Sex.Equals("m") || pacient.Sex.Equals("M")) ? 0 : (pacient.Sex.Equals("f") || pacient.Sex.Equals("F")) ? 1 : 0;
            TXTAddress.Text = pacient.Adresa;
            TXTPhone.Text = pacient.Telefon;
            TXTEmail.Text = pacient.Email;
            TXTAllergies.Text = pacient.Lista_alergi;
            TXTMedicalHistory.Text = pacient.Istoric_medical;
            TXTMedicalAdvice.Text = pacient.Recomandari_medicale;
            TXTPhysiologicalMeasures.Text = pacient.Masurari_fiziologice;
        }

        public void LoadDoctorsData()
        {
            LSTDoctors.Clear();
            List<Medic> doctors = GUIServerConnector.getInstance().getServerInterface().getMedicsData(sessionId);
            LSTDoctors.BeginUpdate();
            foreach (Medic doctor in doctors)
            {
                ListViewItem item = new ListViewItem(doctor.Nume, 5);
                item.SubItems.Add(doctor.Prenume);
                item.SubItems.Add(doctor.Specializare);
                item.SubItems.Add(doctor.Telefon);
                LSTDoctors.Items.Add(item);
            }
            LSTDoctors.EndUpdate();
        }

        public void LoadTreatmentsData()
        {
            LSTTreatments.Clear();
            List<Tratamente> treatments = GUIServerConnector.getInstance().getServerInterface().getTreatments(sessionId);
            LSTTreatments.BeginUpdate();
            foreach (Tratamente treatment in treatments)
            {
                ListViewItem item = new ListViewItem(treatment.Medicamente, 1);
                item.SubItems.Add(treatment.Cantitate);
                item.SubItems.Add(treatment.Interval);
                item.SubItems.Add(treatment.Durata_tratament);
                item.SubItems.Add(treatment.Diagnostic);
                item.SubItems.Add(treatment.Observatii);
                LSTTreatments.Items.Add(item);
            }
            LSTTreatments.EndUpdate();
        }

        public void LoadSensorsData()
        {
            if (sensorDefinitions == null)
                sensorDefinitions = GUIServerConnector.getInstance().getServerInterface().getSensorDefinitions(sessionId);
            LSTSensorValues.Clear();
            List<ValoriSensori> sensorValues = GUIServerConnector.getInstance().getServerInterface().getSensorData(sessionId);
            LSTSensorValues.BeginUpdate();
            foreach (DefinitieSenzori sensorDefinition in sensorDefinitions)
            {
                ListViewItem item = new ListViewItem(sensorDefinition.Tip_Senzori, 2);
                ValoriSensori foundSensorValue = null;
                foreach (ValoriSensori sensorValue in sensorValues)
                {
                    if (sensorValue.idDefinitie.HasValue && (sensorValue.idDefinitie.Value.Equals(sensorDefinition.idDefinitie)))
                    {
                        foundSensorValue = sensorValue;
                        break;
                    }
                }
                if (foundSensorValue != null)
                {
                    MemoryStream valueStream = new MemoryStream(foundSensorValue.ValoareCitita);
                    item.SubItems.Add(binaryFormatter.Deserialize(valueStream).ToString());
                    valueStream.Close();
                }
                else
                {
                    item.SubItems.Add("");
                }
                item.SubItems.Add(sensorDefinition.Unitate_de_masura);
            }
        }

        private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = LSTDoctors.Columns[e.ColumnIndex].Width;
        }

        private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = LSTSensorValues.Columns[e.ColumnIndex].Width;
        }

        private void CMBView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CMBView1.SelectedIndex)
            {
                case 0:
                    LSTDoctors.View = View.LargeIcon;
                    break;
                case 1:
                    LSTDoctors.View = View.List;
                    break;
                case 2:
                    LSTDoctors.View = View.Details;
                    break;
            }
        }

        private void CMBView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CMBView2.SelectedIndex)
            {
                case 0:
                    LSTTreatments.View = View.LargeIcon;
                    break;
                case 1:
                    LSTTreatments.View = View.List;
                    break;
                case 2:
                    LSTTreatments.View = View.Details;
                    break;
            }
        }

        private void CMBView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CMBView3.SelectedIndex)
            {
                case 0:
                    LSTSensorValues.View = View.LargeIcon;
                    break;
                case 1:
                    LSTSensorValues.View = View.List;
                    break;
                case 2:
                    LSTSensorValues.View = View.Details;
                    break;
            }
        }
    }
}
